package d0626;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import oracle.sql.BLOB;

/**
 * 	BLOB  二进制数据: 视频,音频,图片,压缩文档....
 * 	CLOB  TXT, HTML ...
 */
public class 大数据类型操作 {

	/**
	 * 修改员工头像
	 * @param empno	员工编号
	 * @param path	图片地址
	 * @throws ClassNotFoundException 
	 * @throws SQLException 
	 * @throws IOException 
	 */
	public void setHead(int empno, String path) throws ClassNotFoundException, SQLException, IOException {
		String sql = "update emp set headimg=?, headpath=? where empno=?";

		Class.forName("oracle.jdbc.driver.OracleDriver");

		// 获取连接   本地地址 :  localhost(域名)   127.0.0.1(ip地址)  1521 默认端口
		String url = "jdbc:oracle:thin:@//127.0.0.1:1521/orcl";
		String user = "scott";
		String password = "a";
		Connection conn = DriverManager.getConnection(url, user, password);

		PreparedStatement ps1 = conn.prepareStatement(sql);

		ps1.setString(2, path);
		ps1.setInt(3, empno);

		// 文件  字节 输入流     FileInputStream
		File file = new File(path);
		FileInputStream fis = new FileInputStream(file);
		// 定义字节数组
		byte[] b = new byte[(int) file.length()];
		// 读取文件 字节数组
		fis.read(b);
		// 使用字节数组将数据传入
		ps1.setBytes(1, b);

		ps1.executeUpdate();

		conn.close();
		fis.close();

	}

	public void downHead(int empno, String newfile) throws ClassNotFoundException, SQLException, IOException {
		String sql = "select headimg from emp where empno=?";

		Class.forName("oracle.jdbc.driver.OracleDriver");

		// 获取连接   本地地址 :  localhost(域名)   127.0.0.1(ip地址)  1521 默认端口
		String url = "jdbc:oracle:thin:@//127.0.0.1:1521/orcl";
		String user = "scott";
		String password = "a";
		Connection conn = DriverManager.getConnection(url, user, password);

		PreparedStatement ps1 = conn.prepareStatement(sql);
		ps1.setInt(1, empno);

		ResultSet rs = ps1.executeQuery();

		if (rs.next()) {
			byte[] data = rs.getBytes(1);
			FileOutputStream fos = new FileOutputStream(newfile);
			fos.write(data);
			fos.close();
		}

		conn.close();

	}

}
